package com.augurit.gzcgwsp.sendNotice.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.sendNotice.dao.YwMtbjlshInfoDao;
import com.augurit.gzcgwsp.sendNotice.entity.YwMtbjlshInfo;
import com.augurit.gzcgwsp.sendNotice.service.IYwMtbjlshInfoService;

/**
 * 每天办件流水号表--实体
 * @author xu
 *
 */
@Service
@Transactional
public class YwMtbjlshInfoServiceImpl implements IYwMtbjlshInfoService{
	
	@Autowired
	private YwMtbjlshInfoDao ywMtbjlshInfoDao;

	/**
	 * 保存 ywMtbjlshInfo
	 * @param ywMtbjlshInfo 
	 */
	public void saveOrUpdateYwMtbjlshInfo(YwMtbjlshInfo ywMtbjlshInfo) {
		//ywMtbjlshInfoDao.save(ywMtbjlshInfo);
		ywMtbjlshInfoDao.getSession().saveOrUpdate(ywMtbjlshInfo);
	}
	
	/**
	 * 获取 ywMtbjlshInfo
	 * @param id 
	 */
	public YwMtbjlshInfo getYwMtbjlshInfo(Long id) {
		return ywMtbjlshInfoDao.get(id);
	}
	
	/**
	 * 获取所有 流水号
	 * @param date 
	 */
	public List<YwMtbjlshInfo> getAll() {		
		return ywMtbjlshInfoDao.getAll();
	}

	/**
	 * 获取 流水号
	 * @param date 
	 */
	public String getLsh(Date date,Long id) {
		String STR_FORMAT = "0000"; 
		SimpleDateFormat myFmt=new SimpleDateFormat("yyMMdd");		
		String preDate = myFmt.format(date);
//		String preDate = "110608";
		Date todayDateTemp = new Date();
		String todayDate = myFmt.format(todayDateTemp);
		if(id != null && preDate.equals(todayDate)){//如果id不等于空，两个时间相等，则查数据库，把数据库里面那个拿出来，+1
			YwMtbjlshInfo ywMtbjlshInfo = ywMtbjlshInfoDao.get(id);
			STR_FORMAT = ywMtbjlshInfo.getMtLsh();
			STR_FORMAT = this.haoAddOne(STR_FORMAT);//加1
			ywMtbjlshInfo.setMtLsh(STR_FORMAT);
			ywMtbjlshInfo.setRq(todayDateTemp);		
			this.saveOrUpdateYwMtbjlshInfo(ywMtbjlshInfo);
			System.out.print("1-----");
		}else if(id != null && !preDate.equals(todayDate)){//如果id不等于空，两个时间不相等，则把数据库里面的流水号重置
			YwMtbjlshInfo ywMtbjlshInfo = ywMtbjlshInfoDao.get(id);
			STR_FORMAT = "0000";
			ywMtbjlshInfo.setMtLsh(STR_FORMAT);//重置
			ywMtbjlshInfo.setRq(todayDateTemp);		
			this.saveOrUpdateYwMtbjlshInfo(ywMtbjlshInfo);
			System.out.print("2-----");
		}else{//否则，把第一个0000存入数据库
			YwMtbjlshInfo ywMtbjlshInfo = new YwMtbjlshInfo();
			ywMtbjlshInfo.setMtLsh(STR_FORMAT);
			ywMtbjlshInfo.setRq(new Date());
			this.saveOrUpdateYwMtbjlshInfo(ywMtbjlshInfo);
			System.out.println("3-----"+STR_FORMAT);
		}
		return STR_FORMAT;
	}
	
	/**
	 * 流水号加1
	 * @param liuShuiHao 
	 */
	public static String haoAddOne(String liuShuiHao){
	    Integer intHao = Integer.parseInt(liuShuiHao);
	    intHao++;
	    DecimalFormat df = new DecimalFormat("0000");
	    return df.format(intHao);
	}
	
	/**
	 * 获取政务中心统一查询号和密码
	 * @param date
	 * @param ywMtbjlshInfoid
	 * @param sjly 收件来源  网办1或者大厅0
	 * @return
	 */
	public Map<String,String> getSerialNoAndPassword(String sjly){
		
		//获取流水号  --start
		//List<YwMtbjlshInfo>  ywMtbjlshInfoList = this.getAll();
    	Date date = new Date();
		//String lsh = this.getLsh(date, ywMtbjlshInfoid);//每天办事流水号  4位
		int lsh=(int)(Math.random() * 10000);
		//获取流水号  --end
		//生成统一查询号 -- xu
		String qxdm = "gz";//区县代码 2位  固定
		String wjdm = "26";//委局代码 2位  固定
		//获取6位数的日期
    	String rq = DateUtilsEX.getYearAndMonthAndDay();
		StringBuffer sb = new StringBuffer();
		sb.append(qxdm).append(wjdm).append(rq).append(sjly).append(lsh);
		String serialNo = sb.toString();
		
		//生成随机密码  4位
		int passwordTemp = (int)(Math.random()*9000)+1000;
		String password = String.valueOf(passwordTemp);
		
		Map<String,String> serialNoAndPasswordMap = new HashMap<String,String>();
		serialNoAndPasswordMap.put("serialNo", serialNo);
		serialNoAndPasswordMap.put("password", password);
		return serialNoAndPasswordMap;
	}

	
	/**
	 * 获取区的统一查询号和密码
	 * @param date
	 * @param ywMtbjlshInfoid
	 * @param sjly 收件来源  网办1或者大厅0
	 * @return
	 */
	public Map<String,String> getQuSerialNoAndPassword(String sjly,String area){
		
		//获取流水号  --start
		//List<YwMtbjlshInfo>  ywMtbjlshInfoList = this.getAll();
    	Date date = new Date();
		//String lsh = this.getLsh(date, ywMtbjlshInfoid);//每天办事流水号  4位
		int lsh=(int)(Math.random() * 1000);
		//获取流水号  --end
		//生成统一查询号 -- xu
		String qxdm =area;//区县代码 2位  固定
		//String wjdm = "26";//委局代码 2位  固定
		//获取6位数的日期
    	String rq = DateUtilsEX.dateFormate(new Date(), "yyyyMMdd");
		StringBuffer sb = new StringBuffer();
		//sb.append(qxdm).append(wjdm).append(rq).append(sjly).append(lsh);
		//sb.append(qxdm).append(rq).append(sjly).append(lsh);
		sb.append(qxdm).append(rq).append(lsh);
		String serialNo = sb.toString();
		
		//生成随机密码  4位
		int passwordTemp = (int)(Math.random()*9000)+1000;
		String password = String.valueOf(passwordTemp);
		
		Map<String,String> serialNoAndPasswordMap = new HashMap<String,String>();
		serialNoAndPasswordMap.put("serialNo", serialNo);
		serialNoAndPasswordMap.put("password", password);
		return serialNoAndPasswordMap;
	}

	
	@Override
	public Map<String, String> getRealSerialNo(String sjly) {
		//获取流水号  --start
		List<YwMtbjlshInfo>  ywMtbjlshInfoList = this.getAll();
    	Long ywMtbjlshInfoid = null;
    	Date date = new Date();
    	if(ywMtbjlshInfoList.size()>0){
    		ywMtbjlshInfoid = ywMtbjlshInfoList.get(0).getId();
    		date = ywMtbjlshInfoList.get(0).getRq();
    	}    	
		String lsh = this.getLsh(date, ywMtbjlshInfoid);//每天办事流水号  4位
		//获取流水号  --end
		//生成统一查询号 -- xu
		String qxdm = "gz";//区县代码 2位  固定
		String wjdm = "26";//委局代码 2位  固定
//		Date date1 = new Date();
//		SimpleDateFormat myFmt=new SimpleDateFormat("yyMMdd");		
//		String rq = myFmt.format(date1);//日期 6位
		//获取6位数的日期
    	String rq = DateUtilsEX.getYearAndMonthAndDay();
		
		StringBuffer sb = new StringBuffer();
		sb.append(qxdm).append(wjdm).append(rq).append(sjly).append(lsh);
		String serialNo = sb.toString();
		
		//生成随机密码  4位
		int passwordTemp = (int)(Math.random()*9000)+1000;
		String password = String.valueOf(passwordTemp);
		
		Map<String,String> serialNoAndPasswordMap = new HashMap<String,String>();
		serialNoAndPasswordMap.put("serialNo", serialNo);
		serialNoAndPasswordMap.put("password", password);
		return serialNoAndPasswordMap;
	}
	
	
}
